package com.yangming.springboot.controller;


import com.yangming.springboot.RequirePermission;
import com.yangming.springboot.base.ResultInfo;
import com.yangming.springboot.query.SaleChanceQuery;
import com.yangming.springboot.service.SaleChanceService;
import com.yangming.springboot.service.UserService;
import com.yangming.springboot.utils.LoginUserUtil;
import com.yangming.springboot.vo.SaleChance;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "sale_chance")
public class SaleChanceController {

    @Resource
    private SaleChanceService saleChanceService;

    @Resource
    private UserService userService;

    @GetMapping(value = "list")
    @ResponseBody
    @RequirePermission(code = "101001")
    public Map<String, Object> querySaleChanceParams(Integer flag, HttpServletRequest request,  SaleChanceQuery saleChanceQuery){
        if (flag != null && flag == 1){
            //分配给flag=1的属性
            saleChanceQuery.setAssignMan(LoginUserUtil.releaseUserIdFromCookie(request));
        }
        return saleChanceService.querySaleChancesByParams(saleChanceQuery);
    }

    @RequestMapping(value = "index")
    @RequirePermission(code = "1010")
    public String index(){
        return "saleChance/sale_changce";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    @RequirePermission(code = "101004")
    public ResultInfo saveChance(HttpServletRequest request, SaleChance saleChance){
        saleChance.setCreateMan(userService.queryUserById(LoginUserUtil.releaseUserIdFromCookie(request)).getUserName());
        saleChanceService.saveChance(saleChance);
        /**
         * 这个返回new ResultInfo这个方法可以提出来，写成基类BaseController的一个封装好的方法，新的controller就extends BaseController，就不用每次都new一个了
         * 不过现在我懒得写，就先这样了
         * */
        return new ResultInfo();
    }

    @RequestMapping(value = "addSaleChance")
    public String addSaleChancePage(){
        return "salChance";
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public ResultInfo updateSaleChance(SaleChance saleChance){
        saleChanceService.updateSaleChance(saleChance);
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setMsg("update chance success");
        return resultInfo;
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    @RequirePermission(code = "101005")
    public ResultInfo deleteSaleChance(Integer[] ids){
        ResultInfo resultInfo = new ResultInfo();
        saleChanceService.deleteSaleChance(ids);
        resultInfo.setMsg("delete data success");
        return resultInfo;
    }

    @RequestMapping(value = "updateSaleChanceDevResult")
    @ResponseBody
    public ResultInfo updateSaleChanceDevResult(Integer id, Integer devResult){
        saleChanceService.updateSaleChanceDevResult(id, devResult);
        return new ResultInfo("dev success");
    }

}
